package com.xyazm.fulfill.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xyazm.common.dao.BaseDAO;
import com.xyazm.fulfill.domain.entity.OrderFulfillDO;
import com.xyazm.fulfill.mapper.OrderFulfillMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 订单履约表 DAO
 * </p>
 *
 */
@Repository
public class OrderFulfillDAO extends BaseDAO<OrderFulfillMapper, OrderFulfillDO> {


    /**
     * 查询履约单
     * @param orderId
     * @return
     */
    public OrderFulfillDO getOne(String orderId) {
        LambdaQueryWrapper<OrderFulfillDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderFulfillDO::getOrderId,orderId);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 取消履约单
     * @author xuyou
     * 2022/7/30 0030 21:08
     * @param id
    */
    public void cancelFulfill(Long id,Integer status) {
        LambdaUpdateWrapper<OrderFulfillDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OrderFulfillDO::getId,id);
        updateWrapper.set(OrderFulfillDO::getStatus,status);
        baseMapper.update(null,updateWrapper);
    }

    /**
     * 更新配送员信息
     * @author xuyou
     * 2022/8/13 0013 23:09
     * @param fulfillId
     * @param delivererNo
     * @param delivererName
     * @param delivererPhone
     * @return boolean
    */
    public boolean updateDeliverer(String fulfillId,String delivererNo, String delivererName, String delivererPhone) {
        LambdaUpdateWrapper<OrderFulfillDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(OrderFulfillDO::getDelivererNo, delivererNo)
                .set(OrderFulfillDO::getDelivererName, delivererName)
                .set(OrderFulfillDO::getDelivererPhone, delivererPhone)
                .eq(OrderFulfillDO::getFulfillId, fulfillId);
        return update(updateWrapper);
    }

    /**
     * 保存物流单号
     * @author xuyou
     * 2022-08-16 22:53
     * @param fulfillId
     * @param logisticsCode
    */
    public void saveLogisticsCode(String fulfillId, String logisticsCode) {
        LambdaUpdateWrapper<OrderFulfillDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(OrderFulfillDO::getLogisticsCode, logisticsCode)
                .eq(OrderFulfillDO::getFulfillId, fulfillId);
        update(updateWrapper);
    }

    /**
     * 查询履约单
     * @author xuyou
     * 2023-02-09 21:05
     * @param orderId
    */
    public List<OrderFulfillDO> listByOrderId(String orderId) {
        LambdaQueryWrapper<OrderFulfillDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderFulfillDO::getOrderId, orderId);
        return list(queryWrapper);
    }


    /**
     * 更新履约单状态
     */
    public boolean updateStatus(String fulfillId, Integer fromStatus, Integer toStatus) {
        LambdaUpdateWrapper<OrderFulfillDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(OrderFulfillDO::getStatus, toStatus)
                .eq(OrderFulfillDO::getFulfillId, fulfillId)
                .eq(OrderFulfillDO::getStatus, fromStatus);
        return update(updateWrapper);
    }

    /**
     * 批量更新履约单状态
     */
    public boolean batchUpdateStatus(List<String> fulfillIds, Integer fromStatus, Integer toStatus) {
        LambdaUpdateWrapper<OrderFulfillDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(OrderFulfillDO::getStatus, toStatus)
                .in(OrderFulfillDO::getFulfillId, fulfillIds)
                .eq(OrderFulfillDO::getStatus, fromStatus);
        return update(updateWrapper);
    }
}
